/************************************************************************/
/*  									*/
/*  fullsearch_MI.c							*/
/*									*/
/*  Tom Lookabaugh							*/
/*  August 13, 1987							*/
/*  Last Revision: August 15, 1987					*/
/*									*/
/*  DESCRIPTION								*/
/*	Full search through a codebook.					*/
/*	Modified Itakura Version. Note that the reported distortion	*/
/*	is the true modified Itakura distortion.			*/
/*  RETURNS								*/
/*	Returns distortion between trvect and the closest entry in	*/
/*	the codebook.  Puts index of closest entry into storage pointed	*/
/*	to by bestcdwd.							*/
/*  CALLS								*/
/*	none.								*/
/*									*/
/************************************************************************/

#include "stdvq.h"
#include <math.h>

double fullsearch(cdbk, cdbksz, trvect, bestcdwd)

    double *cdbk;	/* codebook					*/
    int cdbksz;		/* number of vectors in codebook		*/
    double *trvect;	/* training sequence vector			*/
    int *bestcdwd;	/* points to index of closest codeword		*/

{

    int c, d;		/* iteration counters				*/
    double presdist;	/* present distortion				*/
    double bestdist;	/* best distortion				*/
	
    bestdist = HUGE;

    for (c = 0; c < cdbksz; c++) {
	presdist = 0.0;
	for (d = 2; d < dim; d++) {
	    presdist += trvect[d]*cdbk[c*cdbkdim + d - 1];
	}
	presdist = 2.0*presdist + trvect[1]*cdbk[c*cdbkdim];
	if (presdist <  bestdist) {
	    bestdist = presdist;
	    *bestcdwd = c;
	}
    }
    bestdist = log(bestdist/trvect[0]);
    return(bestdist);
}
	
